import java.util.Arrays;

public class Main {
    
    public static void main(String[] args) {
        //二维数组
        int[][] array1 = {{1,2,3},{4,5,6}};
        //int[][] array2 = new int[][]{{1,2,3},{4,5,6}};
        //int[][] array3 = new int[2][3];
        System.out.println(array1.length);//行数
        System.out.println(array1[0].length);
        System.out.println(array1[0].length);
        System.out.println(Arrays.toString(array1[0]));
        System.out.println(Arrays.toString(array1[1]));
        System.out.println("============");
        for (int i = 0; i < array1.length; i++) {
            for (int j = 0; j < array1[i].length; j++) {
                System.out.print(array1[i][j] +" ");
            }
            System.out.println();
        }
        System.out.println("============");
        for (int[] arr: array1) {
            for (int i = 0; i < arr.length; i++) {
                System.out.print(arr[i] +" ");
            }
            System.out.println();
        }
        System.out.println("============");
        String ret = Arrays.deepToString(array1);
        System.out.println(ret);
    }
    public static void main5(String[] args) {
        //int[] array1 = {1,2,3,4,5,6};
        //int[] array2 = {1,2,3,4,5,6};
        //System.out.println(array1 == array2);//比较地址
        //boolean len = Arrays.equals(array1,array2);//比较两个数组的内容，返回 boolean 类型
        //System.out.println(len);

        int[] ret = new int[5];
        System.out.println(Arrays.toString(ret));
        //Arrays.fill(ret,-1);//把 ret 数组都置为 -1
        Arrays.fill(ret,1,3,-1);//把 [1,3) 下标置为 -1
        System.out.println(Arrays.toString(ret));
    }
    public static int[] copyArray(int[] array) {
        //自实现数组拷贝
        int[] copy = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            copy[i] = array[i];
        }
        return copy;
    }
    public static void main4(String[] args) {
        int[] array = {1, 3, 4, 2, 6, 8, 7, 9};
        //int[] copy = Arrays.copyOfRange(array,1,3);//从[1,3)下标开始拷贝
        //int[] copy = Arrays.copyOf(array,array.length*2);//二倍扩容
        //int[] copy = Arrays.copyOf(array,array.length);//正常拷贝
        //int[] copy = copyArray(array);
        int[] copy = new int[array.length];
        System.arraycopy(array,0,copy,0,array.length);
        //把array从下标 0 开始拷贝到 copy数组中从0开始长度为array.length
        System.out.println(Arrays.toString(copy));
    }
    public static void bubblesort(int[] array) {
        for (int i = 0; i < array.length - 1; i++) {
            boolean flag = false;//优化
            for (int j = 0; j < array.length - 1 - i; j++) {
                if(array[j] > array[j + 1]) {
                    int temp = array[j + 1];
                    array[j + 1] = array[j];
                    array[j] = temp;
                    flag = true;//排序一次后就把flag给为true
                }
            }
            if(flag = false) {
                return;//已经有序，直接返回
            }
        }
    }
    public static void main3(String[] args) {
        int[] array = {5, 4, 9, 3, 1, 8, 6, 7, 10, 2};
        System.out.println("排序前："+ Arrays.toString(array));
        bubblesort(array);
        System.out.println("排序后："+ Arrays.toString(array));
    }
    public static void main2(String[] args) {
        int[] array = {1, 5, 6, 85, 40, 91, 32};
        System.out.println(Arrays.toString(array));
        Arrays.sort(array);
        System.out.println(Arrays.toString(array));//排序后
        //int ret = Arrays.binarySearch(array,40);//二分查找
        int ret = Arrays.binarySearch(array,1,3,6);//在下标为1-3中找6,范围是[1,3),左闭右开。
        System.out.println(ret);
    }
    public static String myToString(int[] array) {
    //方法只能在类里，方法：public static 返回类型 方法名(变量参数)
        String str = "[";
        for (int i = 0; i < array.length; i++) {
            str += array[i];
            if(i != array.length - 1) {
                //在最后一个的时候不加“, ”
                str += ", ";
            }
        }
        str += "]";
        return str;
    }
    public static void main1(String[] args) {
        //模拟实现toString
        int[] array = {1, 2, 3, 4, 5};
        System.out.print(myToString(array));
    }
}
